<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__MANAGE__lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="__MANAGE__lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="__MANAGE__css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">文章标题</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="required" lay-reqtext="文章标题不能为空" placeholder="请输入文章标题" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">分类栏目</label>
        <div class="layui-input-block">
            <select name="cateid" lay-verify="required" lay-reqtext="栏目不能为空">
                <option value=""></option>
                {volist name="cate" id="vo"}
                <option value="{$vo.id}"><?php if(isset($vo['level'])){echo str_repeat("|--",$vo['level']);}?>{$vo.title}</option>
                {/volist}
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">远程跳转链接</label>
        <div class="layui-input-block">
            <input type="text" name="url"  placeholder="远程跳转链接" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">关键字</label>
        <div class="layui-input-block">
            <input type="text" name="keyword"  placeholder="请输入关键字" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">设置时间</label>
        <div class="layui-input-block">
            <input name="time" type="text" class="layui-input" id="test5" placeholder="不设置就是发布时间">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文章摘要</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入文章摘要" name="desc" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">展现数据</label>
        <div class="layui-input-block">
            <input type="text" name="click_wai"  placeholder="请输入展现数据，不是真实的" value="<?php echo mt_rand(500,2000); ?>" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文章来源</label>
        <div class="layui-input-block">
            <input type="text" name="laiyuan"  placeholder="请输入文章来源" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文章作者</label>
        <div class="layui-input-block">
            <input type="text" name="author" placeholder="请输入文章作者" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">设为推荐</label>
        <div class="layui-input-block">
            <input type="checkbox" name="state" lay-skin="switch">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">缩略图</label>
        <div class="layui-input-block">
            <div class="layui-upload">
                <button type="button" class="layui-btn test">上传图片</button>
                <input type="hidden" name="pic" value="" class="lizhili_hidden">
                <div class="layui-upload-list">
                    <img class="layui-upload-img lizhili" src="" style="width: 200px">
                </div>
            </div>
        </div>
    </div>


    <div class="layui-form-item dan">
        <label class="layui-form-label">文章内容</label>
        <div class="layui-input-block">
            <textarea id="demo" style="display: none;"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">开关</label>
        <div class="layui-input-block">
            <input type="checkbox" name="isopen" checked lay-skin="switch">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="__MANAGE__lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="__MANAGE__js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['upload','laydate','form','layedit'], function () {
        var form = layui.form,
            layer = layui.layer,
            laydate=layui.laydate,
            upload = layui.upload,
            $ = layui.$;
        var layedit = layui.layedit;
        laydate.render({
            elem: '#test5'
            ,type: 'datetime'
        });
        var uploadInst = upload.render({
            elem: '.test'
            ,url: 'update?lizhili=123456'
            ,accept:'images'
            ,before: function(obj){
                layer.msg('上传中', {icon: 16, time: 0});
                let that=this
                obj.preview((index, file, result)=>{
                    that.item.parent().find('.lizhili').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }

                this.item.parent().find('.lizhili_hidden').val(res.data.src);
                return layer.msg('上传成功');
            }
        });

        layedit.set({
            uploadImage: {
                url: 'update?lizhili=123456' //接口url
                ,type: '' //默认post
            }
        });

       let index= layedit.build('demo',{
            tool: [
                'strong' //加粗
                ,'italic' //斜体
                ,'underline' //下划线
                ,'del' //删除线
                ,'|' //分割线
                ,'left' //左对齐
                ,'center' //居中对齐
                ,'right' //右对齐
                ,'link' //超链接
                ,'unlink' //清除链接
                ,'image' //插入图片
            ]
        }); //建立编辑器

        form.on('select(type)', function(data){
            if(data.value==2){
                $('.dan').show()
                $('lian').hide()
            }else if(data.value==4){
                $('.dan').hide()
                $('.lian').show()
            }else {
                $('.dan').hide()
                $('.lian').hide()
            }
        })

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data.field.text=layedit.getContent(index)
            $.post("api",
            {
                data:data.field,
                'type':'add'
            },
            function(res,status){
               if(res.code==0){
                  //  var iframeIndex = parent.layer.getFrameIndex(window.name);
                  // parent.layer.close(iframeIndex);
                   layer.msg(res.msg, {icon: 1});
                   setTimeout(function(){
                       window.parent.location.reload();//修改成功后刷新父界面
                   }, 300);
               }else{
                   layer.msg(res.msg, {icon: 5});
               }
            });

        });


    });
</script>
</body>
</html>